package com.mediacloud.app.newsmodule.fragment.baoliao.holder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.CallSuper;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.assembly.util.FunKt;
import com.mediacloud.app.assembly.util.Reflect;
import com.mediacloud.app.assembly.util.StringUtils;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoListItem;
import com.mediacloud.app.model.utils.DefaultBgUtil;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.baoliao.activity.BaoLiaoDetailActivity;
import com.mediacloud.app.newsmodule.fragment.baoliao.adapter.BaoLiaoRankListAdapter;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;
import com.zimeiti.view.CollapsedTextViewAppfac;

public class BaoLiaoRankListPureTxtHolder extends RecyclerView.ViewHolder
        implements BaoLiaoRankListAdapter.BaoliaoInterface
        , CollapsedTextViewAppfac.ShowHideAllListener {
    ImageView baoniaoRenNaoKe;
    TextView baoniaoren;
    TextView baoniaoshijian;
    public CollapsedTextViewAppfac baoniaobiaoti;
    TextView baoniaodianzanshu, baoniaodianpinglunshu, baoLiaoCommentNum;
    private Drawable defaultLoading;
    Reflect reflect;
    private TextView baoniaoTitle;
    private TextView baoniaoCategroy;
    public View moreIcon;
    public TextView rankTv;
    public CardView cardView;

    public BaoLiaoRankListPureTxtHolder(View itemView) {
        super(itemView);
        defaultLoading = AppFactoryGlobalConfig.getDefaultImageLoadDrawable();
        rankTv = itemView.findViewById(R.id.text_rank);
        baoniaoRenNaoKe = itemView.findViewById(R.id.baoniaoRenNaoKe);
        baoniaoTitle = itemView.findViewById(R.id.baoniaoTitle);
        baoniaoCategroy = itemView.findViewById(R.id.tv_bao_liao_lan_mu);
        baoniaoren = itemView.findViewById(R.id.baoniaoren);
        baoniaoren.setTextColor(DefaultBgUtil.getappfacTitleColor(itemView.getContext()));
        baoniaoshijian = itemView.findViewById(R.id.baoniaoshijian);
        baoniaobiaoti = itemView.findViewById(R.id.baoniaobiaoti);
        cardView = itemView.findViewById(R.id.card_view);

        reflect = Reflect.on(baoniaobiaoti);
        baoniaodianzanshu = itemView.findViewById(R.id.baoniaodianzanshu);
        baoniaodianpinglunshu = itemView.findViewById(R.id.baoniaodianpinglunshu);
        baoLiaoCommentNum = itemView.findViewById(R.id.baoLiaoCommentNum);
        moreIcon = itemView.findViewById(R.id.moreIcon);
    }

    @SuppressLint("SetTextI18n")
    @CallSuper
    @Override
    public void updateData(final BaoNiaoListItem item, int position, final Context context, boolean isRegular) {
//        GlideUtils.loadUrl(item.getAvatar(),baoniaoRenNaoKe,null, defaultLoading);
        // GlideUtils.loadUrl(item.getAvatar(), baoniaoRenNaoKe, null, defaultLoading, false, true);
        FunKt.loadRound(baoniaoRenNaoKe, item.getAvatar(), defaultLoading, null);
        baoniaoren.setText(item.getNickName());
        baoniaoshijian.setText(item.getAddTime_format());
        baoniaobiaoti.showHideAllListener = this;
        baoniaobiaoti.mShowWidth = 0;
        baoniaobiaoti.mIsExpanded = item.showAll;
        baoniaobiaoti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setClick(item, context);
            }
        });
        if (!isRegular) {
            baoniaobiaoti.setTipsClickable(true);
        } else {
            baoniaobiaoti.setTipsClickable(false);
        }

        if (TextUtils.isEmpty(item.getContent())) {
            baoniaobiaoti.setText("");
            baoniaobiaoti.setVisibility(View.GONE);
        } else {
            baoniaobiaoti.setVisibility(View.VISIBLE);
            baoniaobiaoti.setText(item.getContent());
        }
        int mainColor = DefaultBgUtil.getTintColor(context);
        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_baoliao_rank);
        Drawable drawable2 = ContextCompat.getDrawable(context, R.drawable.bg_baoliao_label);
        if (position <= 9) {
            rankTv.setVisibility(View.VISIBLE);
            rankTv.setText((position + 1) + "");
            if (isRegular) {
                rankTv.setTextColor(Color.WHITE);
                if (position == 0) {
                    rankTv.setBackground(Utility.tintDrawable(Color.parseColor("#FFFF4141"), drawable));
                } else if (position == 1) {
                    rankTv.setBackground(Utility.tintDrawable(Color.parseColor("#FFFFA245"), drawable));

                } else if (position == 2) {
                    rankTv.setBackground(Utility.tintDrawable(Color.parseColor("#FFFFCF4F"), drawable));
                } else {
                    rankTv.setBackground(Utility.tintDrawable(mainColor, drawable));
                }
            } else {
                if (position == 0) {
                    rankTv.setTextColor(Color.WHITE);
                    rankTv.setBackground(Utility.tintDrawable(Color.parseColor("#FFFF4141"), drawable2));
                } else if (position == 1) {
                    rankTv.setTextColor(Color.WHITE);
                    rankTv.setBackground(Utility.tintDrawable(Color.parseColor("#FFFFA245"), drawable2));
                } else if (position == 2) {
                    rankTv.setTextColor(Color.WHITE);
                    rankTv.setBackground(Utility.tintDrawable(Color.parseColor("#FFFFCF4F"), drawable2));
                } else {
                    rankTv.setTextColor(mainColor);
                    rankTv.setBackground(Utility.tintDrawable(Color.parseColor("#FFEBEBEB"), drawable2));
                }
            }
        } else {
            rankTv.setVisibility(View.GONE);

        }

        moreIcon.setVisibility(View.GONE);
        if (TextUtils.isEmpty(item.getTitle())) {
            baoniaoTitle.setText("");
        } else {
            baoniaoTitle.setText("#" + item.getTitle() + "#");
        }
        if (TextUtils.isEmpty(item.getCatalogName())) {
            baoniaoCategroy.setVisibility(View.GONE);
        } else {
            //UI显示不需要爆料类型
            baoniaoCategroy.setText(item.getCatalogName() + "|");
            baoniaoCategroy.setTextColor(AppFactoryGlobalConfig.getAppServerConfigInfo(baoniaoTitle.getContext()).getMainColor());
            baoniaoCategroy.setVisibility(View.GONE);
        }
        if (baoLiaoCommentNum != null) {
            baoniaodianzanshu.setText(StringUtils.toTenThousand(item.getFavorCount()) + "赞");
            baoniaodianpinglunshu.setText(StringUtils.toTenThousand(TextUtils.isEmpty(item.getHitCount())?"0":item.getHitCount()) + "浏览");
            baoLiaoCommentNum.setText(StringUtils.toTenThousand(item.getCommentCount()) + "评");
        } else {
            baoniaodianzanshu.setText(StringUtils.toTenThousand(item.getFavorCount()) + "");
            baoniaodianpinglunshu.setText(StringUtils.toTenThousand(TextUtils.isEmpty(item.getHitCount())?"0":item.getHitCount()) + "");
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setClick(item, context);
//                context.startActivity(intent);
            }
        });
    }

    public void setClick(final BaoNiaoListItem item, Context context) {
        if (item.getIs_display() == 0) {
            Log.w("APPTAG", "审核没过的  不能点");
            return;
        }
        Intent intent = new Intent(context, BaoLiaoDetailActivity.class);
        intent.putExtra("id", item.getId());
        intent.putExtra("status", item.getStatus());
        intent.putExtra("url", item.getShareHtml());
        NewsItemClickUtils.startActivity(context, intent);
    }

    @Override
    public void appfacShowHide(boolean showAll) {
        BaoNiaoListItem baoNiaoListItem = itemView.getTag(R.id.tagSaveId) != null ? (BaoNiaoListItem) itemView.getTag(R.id.tagSaveId) : null;
        if (baoNiaoListItem != null) {
            baoNiaoListItem.showAll = showAll;
        }
    }
}